import menus from './menus'
// ['0-0', '0-1-1', '0-2', '0-2-2'] ===>  ['0-0', '0-1', '0-1-1', '0-2', '0-2-2']
// 截取字符串  数组合并   数组去重
export function getCheckedKeys (checkedKeys) {
  const arr = []
  checkedKeys.forEach(item => {
    arr.push(item.slice(0, 3))
  })
  const result = Array.from(new Set([...checkedKeys, ...arr])).sort()
  return result
}

// 获取左侧的菜单栏数据
export function getMenu (menus,checkedKeys) {
  let arr = []
  checkedKeys.forEach(item => {
    menus.forEach(value => {
      if(value.key === item) {
        // arr.push({...value})
        arr.push(Object.assign({}, value))
      }
    })
  })
  // 搞定第二层级
  arr.forEach(item => {
    if(item.children) {
      let newArr = getMenu(item.children,checkedKeys)
      item.children = newArr
    }
  })
  return arr
}

// 判断pathname是不是在menus中
export function checkPathName (menus, pathname) {
  // console.log(menus)
  const flag =  menus.some(item => {
    if (item.children) {
      return checkPathName(item.children, pathname)
    } else {
      return item.path === pathname
    }
  })
  // console.log(flag)
  return flag
}